
import requests
from bs4 import BeautifulSoup
import sqlite3
import time
list1=['ddbx','ffd','zh','fcz','kck','dfzd']


def getel(id):
    url = f'https://www.chongso.com/z/chongwu{id}/'
    rq = requests.get(url)
    rq.encoding = rq.apparent_encoding;
    soup = BeautifulSoup(rq.text,'html.parser')
    tag1 = soup.find_all('div',class_='dq_dbox')
    tag2 = soup.find_all('div',class_='dq_dtwo clearfix')
    tag3 = soup.find_all('div',class_='tab-pal')
    text = []
    msg = [] #宠物自我介绍存放
    his = [] #历史来源
    fit = [] #适合人群
    for line1 in tag1:
        line = line1.find_all('div',class_='plc')
        for i in line:
            if i.string is not None:
                text.append(i.string)
    for lines in tag1:
        line = lines.find_all('span')
        for i in line:
            if i.string is not None:
                text.append(i.string)
    #宠物自我介绍
    # for line in tag3:
    #     res1=line.find_all('p')[0]
    #     msg.append((res1.string).strip('\r\n\t'))
    try:
        for line in tag3:
            res1=line.find_all('p')[0]
            msg.append((res1.string).strip('\r\n\t'))
        for line in tag3:
            res1=line.find_all('p')[1]
            try:
               his.append((res1.string).strip('\r\n\t'))
            except:
                his.append('没有数据')    
        for line in tag3:
            res1=line.find_all('p')[2]
            fit.append((res1.string).strip('\r\n\t'))
    except:
        his.append('没有找到信息')
        fit.append('没有找到信息')
        msg.append('没有找到信息')
        print('没有找到')
    for line2 in tag2:
        line = line2.find_all('em')
        for i in line:
            indexnum=str(i).index(':') #16
            res=str(i)[indexnum+1:indexnum+4]
            if res.endswith('%'):

                text.append(res.strip('%'))
            else:
                text.append(res)
    pname=text[0]  #宠物名
    oname =text[1] #宠物别名
    address =text[2] #宠物来源地
    ability='家养猫' #宠物功能
    pmsg =msg[0] #自我介绍
    pet_his=his[0] #宠物历史
    pet_fit=fit[0] #适合人群
    pet_clingy =text[30] #粘人程度
    pet_kind=text[35] #友善
    pet_smart = text[44]#聪明
    pet_honest=text[45]#忠诚
    bueaty=text[46] #颜值
    return pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty


#数据库处理部分

def insert_data(pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty):
  conn = sqlite3.connect('test.sqlite')
  sql = "insert into pet_msg (pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty) values\
  ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty)
  try:
    cursor = conn.cursor()
    cursor.execute(sql)
    return '添加成功'
  finally:
    conn.commit()
    conn.close()


if __name__=='__main__':
    for j,i in zip(list1,range(len(list1))):
        pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,beauty=getel(j)
        res=insert_data(pname=pname, oname=oname, address=address, ability=ability, pmsg=pmsg, pet_his=pet_his,
                    pet_fit=pet_fit,\
                    pet_clingy=pet_clingy, pet_kind=pet_kind, pet_smart=pet_smart, pet_honest=pet_honest, bueaty=beauty)
        print('第%d次,数据成功插入一条'%(i+1))
        print(res)
        time.sleep(1.0)

